package com.example.servlet; /**
 * @author YAO
 * @create 2021-04-19 19:18
 */

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;

public class ParameterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------doGet-------------");
        //获取请求参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String[] hobbies = request.getParameterValues("hobby");

        //1 先以 iso8859-1 进行编码
        //2 再以 utf-8 进行解码
        username = new String(username.getBytes("iso-8859-1"), "UTF-8");

        System.out.println("用户名: " + username);
        System.out.println("密码: " + password);
        System.out.println("爱好: " + Arrays.asList(hobbies));
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
        req.setCharacterEncoding("UTF-8");
        System.out.println("----------doPost-------------");
        //获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbies = req.getParameterValues("hobby");

        System.out.println("用户名: " + username);
        System.out.println("密码: " + password);
        System.out.println("爱好: " + Arrays.asList(hobbies));
    }
}
